package jhsys.mc.device;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import jhsys.mc.smarthome.data.DeviceConfig;
import jhsys.mc.smarthome.data.DeviceUtil;

public class YYSP {
	private String name;
	private String ico;
	private int type;
	private int id;
	private int lcid;
	private int fjid;
	private int rfid;
	private short value;
	private short state;
	private short x;
	private short y;
	private String xmlFileName;//100 bytes
	private String keyFileName;//100 bytes
	private short controlerPort;
	private short controlerType;
    public int baudRate;
    public short parity;
    public short dataBit;
    public short stopBit;
    public short flowControl;
	
	public String getXmlFileName() {
		return xmlFileName;
	}
	public void setXmlFileName(String xmlFileName) {
		this.xmlFileName = xmlFileName;
	}
	public String getKeyFileName() {
		return keyFileName;
	}
	public void setKeyFileName(String keyFileName) {
		this.keyFileName = keyFileName;
	}
	public short getControlerPort() {
		return controlerPort;
	}
	public void setControlerPort(short controlerPort) {
		this.controlerPort = controlerPort;
	}
	public short getControlerType() {
		return controlerType;
	}
	public void setControlerType(short controlerType) {
		this.controlerType = controlerType;
	}
	public int getBaudRate() {
		return baudRate;
	}
	public void setBaudRate(int baudRate) {
		this.baudRate = baudRate;
	}
	public short getParity() {
		return parity;
	}
	public void setParity(short parity) {
		this.parity = parity;
	}
	public short getDataBit() {
		return dataBit;
	}
	public void setDataBit(short dataBit) {
		this.dataBit = dataBit;
	}
	public short getStopBit() {
		return stopBit;
	}
	public void setStopBit(short stopBit) {
		this.stopBit = stopBit;
	}
	public short getFlowControl() {
		return flowControl;
	}
	public void setFlowControl(short flowControl) {
		this.flowControl = flowControl;
	}


	
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIco() {
		return ico;
	}
	public void setIco(String ico) {
		this.ico = ico;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getLcid() {
		return lcid;
	}
	public void setLcid(int lcid) {
		this.lcid = lcid;
	}
	public int getFjid() {
		return fjid;
	}
	public void setFjid(int fjid) {
		this.fjid = fjid;
	}
	public int getRfid() {
		return rfid;
	}
	public void setRfid(int rfid) {
		this.rfid = rfid;
	}
	public short getValue() {
		return value;
	}
	public void setValue(short value) {
		this.value = value;
	}
	public short getState() {
		return state;
	}
	public void setState(short state) {
		this.state = state;
	}
	public short getX() {
		return x;
	}
	public void setX(short x) {
		this.x = x;
	}
	public short getY() {
		return y;
	}
	public void setY(short y) {
		this.y = y;
	}


	public static List<YYSP> read(){
		List<YYSP> result=new ArrayList<YYSP>();
		String filename=DeviceConfig.filePath+DeviceConfig.YYSP_filename;
		RandomAccessFile randomA = null;
		try {
			randomA = new RandomAccessFile(filename, "rw");
			byte[] lclb=new byte[304];
			while(randomA.read(lclb)==304){
				YYSP temp=new YYSP();
				temp.setName(DeviceUtil.byteToString(lclb, 0, 20));
				temp.setIco(DeviceUtil.byteToString(lclb, 20, 40));
				temp.setType(DeviceUtil.byte4ToInt(lclb, 60));
                temp.setId(DeviceUtil.byte4ToInt(lclb, 64));
                temp.setLcid(DeviceUtil.byte4ToInt(lclb, 68));
                temp.setFjid(DeviceUtil.byte4ToInt(lclb, 72));
                temp.setRfid(DeviceUtil.byte4ToInt(lclb, 76));
                temp.setValue(DeviceUtil.byte2ToShort(lclb, 80));
                temp.setState(DeviceUtil.byte2ToShort(lclb, 82));
                temp.setX(DeviceUtil.byte2ToShort(lclb, 84));
                temp.setY(DeviceUtil.byte2ToShort(lclb, 86));
                temp.setXmlFileName(DeviceUtil.byteToString(lclb, 88, 100));
                temp.setKeyFileName(DeviceUtil.byteToString(lclb, 188, 100));
                temp.setControlerPort(DeviceUtil.byte2ToShort(lclb, 288));
                temp.setControlerType(DeviceUtil.byte2ToShort(lclb, 290));
                temp.setBaudRate(DeviceUtil.byte4ToInt(lclb, 292));
                temp.setParity(DeviceUtil.byte2ToShort(lclb, 296));
                temp.setDataBit(DeviceUtil.byte2ToShort(lclb, 298));
                temp.setStopBit(DeviceUtil.byte2ToShort(lclb, 300));
                temp.setFlowControl(DeviceUtil.byte2ToShort(lclb, 302));
				result.add(temp);
				lclb=new byte[304];
			}
			randomA.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
		
	}
	
	
	

	
	
	public static void write(List<YYSP> list){
		RandomAccessFile randomA = null;
		try {
			File file = new File(DeviceConfig.filePath
					+ DeviceConfig.YYSP_filename);
			if (file.exists()) {
				file.delete();
				file.createNewFile();
			}
			randomA = new RandomAccessFile(file, "rw");
			byte[] fjsp;
			for (YYSP item : list) {
				fjsp = new byte[304];
				System.arraycopy(DeviceUtil.stringTobyte(item.getName()), 0, fjsp, 0, 20);
				System.arraycopy(DeviceUtil.stringTobyte(item.getIco(),40), 0, fjsp, 20, 40);
				System.arraycopy(DeviceUtil.unsignedbyteTobyte(item.getType()), 0, fjsp, 60, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getId()), 0, fjsp, 64, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getLcid()), 0, fjsp, 68, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getFjid()), 0, fjsp, 72, 4);
				System.arraycopy(DeviceUtil.intTobytes(item.getRfid()), 0, fjsp, 76, 4);

				System.arraycopy(DeviceUtil.shortTobytes(item.getValue()), 0, fjsp, 80, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getState()), 0, fjsp, 82, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getX()), 0, fjsp, 84, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getY()), 0, fjsp, 86, 2);
				System.arraycopy(DeviceUtil.stringTobyte(item.getXmlFileName(),100), 0, fjsp, 88, 100);
				System.arraycopy(DeviceUtil.stringTobyte(item.getKeyFileName(),100), 0, fjsp, 188, 100);
				System.arraycopy(DeviceUtil.shortTobytes(item.getControlerPort()), 0, fjsp, 288, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getControlerType()), 0, fjsp, 290, 2);
				System.arraycopy(DeviceUtil.intTobytes(item.getBaudRate()), 0, fjsp, 292, 4);
				
				System.arraycopy(DeviceUtil.shortTobytes(item.getParity()), 0, fjsp, 296, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getDataBit()), 0, fjsp, 298, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getStopBit()), 0, fjsp, 300, 2);
				System.arraycopy(DeviceUtil.shortTobytes(item.getFlowControl()), 0, fjsp, 302, 2);
				
				randomA.write(fjsp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != randomA) {
				try {
					randomA.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String toString() {
		return "YYSP [baudRate=" + Integer.toHexString(baudRate) + ", controlerPort="
				+ Integer.toHexString(controlerPort) + ", controlerType=" + Integer.toHexString(controlerType)
				+ ", dataBit=" + Integer.toHexString(dataBit) + ", fjid=" + Integer.toHexString(fjid) + ", flowControl="
				+ Integer.toHexString(flowControl) + ", ico=" + ico + ", id=" + Integer.toHexString(id)
				+ ", keyFileName=" + keyFileName + ", lcid=" + Integer.toHexString(lcid) + ", name="
				+ name + ", parity=" + Integer.toHexString(parity) + ", rfid=" + Integer.toHexString(rfid) + ", state="
				+ Integer.toHexString(state) + ", stopBit=" + Integer.toHexString(stopBit) + ", type=" + Integer.toHexString(type)
				+ ", value=" + Integer.toHexString(value) + ", x=" + Integer.toHexString(x) + ", xmlFileName="
				+ xmlFileName + ", y=" + Integer.toHexString(y) + "]";
	}
	

}
